//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.farm.core.page.OperateType;
import com.farm.core.page.ViewMode;
import com.farm.core.time.TimeTool;
import com.farm.doc.domain.Docdraft;
import com.farm.doc.service.DocdraftServiceInter;
import com.farm.doc.service.DocdraftServiceInter.DRAFT_TYPE;
import com.farm.web.WebUtils;

@RequestMapping({"/docdraft"})
@Controller
public class DocdraftController extends WebUtils {
    private static final Logger log = Logger.getLogger(DocdraftController.class);
    @Resource
    DocdraftServiceInter docDraftServiceImpl;

    public DocdraftController() {
    }

    @RequestMapping({"/addKnow"})
    @ResponseBody
    public Map<String, Object> addKnowSubmit(String text, HttpSession session) {
        if (text == null) {
            return ViewMode.getInstance().setOperate(OperateType.ADD).setError("文本为空，可能是因为文本太大无法保存到草稿箱，请及时提交保存！").returnObjMode();
        } else {
            try {
                DRAFT_TYPE type = DRAFT_TYPE.KNOW_DOC;
                Docdraft draft = this.docDraftServiceImpl.getDocdraftEntityByType(type, this.getCurrentUser(session).getId());
                if (draft == null) {
                    draft = new Docdraft();
                    draft.setContent(text);
                    draft.setType(type.getKey());
                    draft = this.docDraftServiceImpl.insertDocdraftEntity(draft, this.getCurrentUser(session));
                } else {
                    draft.setContent(text);
                    draft = this.docDraftServiceImpl.editDocdraftEntity(draft, this.getCurrentUser(session));
                }

                return ViewMode.getInstance().setOperate(OperateType.ADD).putAttr("entity", draft).returnObjMode();
            } catch (Exception var5) {
                log.error(var5.getMessage());
                return ViewMode.getInstance().setOperate(OperateType.ADD).setError(var5.getMessage()).returnObjMode();
            }
        }
    }

    @RequestMapping({"/findKnow"})
    @ResponseBody
    public Map<String, Object> findKnow(HttpSession session) {
        try {
            DRAFT_TYPE type = DRAFT_TYPE.KNOW_DOC;
            Docdraft draft = this.docDraftServiceImpl.getDocdraftEntityByType(type, this.getCurrentUser(session).getId());
            draft.setEtime(TimeTool.getFormatTimeDate12(draft.getEtime(), "yyyy-MM-dd HH:mm:ss"));
            return ViewMode.getInstance().setOperate(OperateType.ADD).putAttr("entity", draft).returnObjMode();
        } catch (Exception var4) {
            log.error(var4.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.ADD).setError(var4.getMessage()).returnObjMode();
        }
    }

    @RequestMapping({"/delKnow"})
    @ResponseBody
    public Map<String, Object> delKnow(HttpSession session) {
        try {
            DRAFT_TYPE type = DRAFT_TYPE.KNOW_DOC;
            this.docDraftServiceImpl.deleteDocdraft(type, this.getCurrentUser(session).getId());
            return ViewMode.getInstance().setOperate(OperateType.ADD).returnObjMode();
        } catch (Exception var3) {
            log.error(var3.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.ADD).setError(var3.getMessage()).returnObjMode();
        }
    }
}
